
35. Using Databases
	35.3 Create the database with location, comments, and metadata information:
		CREATE DATABASE IF NOT EXISTS myhivedatabase
		COMMENT 'my hive database'
		LOCATION '/user/root/hivedatabase'
		WITH DBPROPERTIES ('creator'='tester','date'='2016-10-05');
	35.1 To show available databases on the system, issue this command in the Hive shell:
		hive>SHOW DATABASES;
	35.2 The USE command sets the current working database, e.g:
		hive>USE default;
	35.3 To drop a database, issue this command:
		DROP (DATABASE|SCHEMA)[IF EXISTS] database_name
		DROP DATABASE IF EXISTS myhivedatabase CASCADE;

41. Example of the CREATE TABLE Statement
	41.1 Prepare the data as follows:
		Use any text editor to create the file employee.txt and add the following data into employee.txt file:
		
		Michael|Shanghai,Toronto|Male,30|DB:80|Product:Developer^DLead
		Peter|New York|Male,35|Perl:85|Product:Lead,Test:Lead
		Shelley|New York|Female,27|Python:80|Test:Lead,COE:Architect
		Lucy|Vancouver|Female,57|Sales:89,HR:94|Sales:Lead
		
	Note:
	The ^D should be input lie the following:
	Press ALT and Hold ALT, then press 004	
		
	41.1 CREATE TABLE employee(
			name string		COMMENT 'name', 
			work_place ARRAY<string>	COMMENT 'Work Place',
			gender_age STRUCT<gender:string,age:int>	COMMENT 'Gender and Age',
			skills_score MAP<string,int>	COMMENT 'Skill Score',
			depart_title MAP<string,ARRAY<string>>	COMMENT 'Department and Title')
		ROW FORMAT DELIMITED FIELDS TERMINATED BY '|'
		COLLECTION ITEMS TERMINATED BY ','
		MAP KEYS TERMINATED BY ':';		
	
	41.1 DESCRIBE employee;

	41.1 DESCRIBE extended employee;
	
	41.1 LOAD DATA LOCAL INPATH '/root/TrainingOnHDP/dataset/employee.txt' OVERWRITE INTO TABLE employee;	
	
	41.1 SELECT * FROM employee;
	
	41.3 CREATE TABLE employee_id (
			name string,
			employee_id int,
			work_place ARRAY<string>,
			sex_age STRUCT<sex:string,age:int>,
			skills_score MAP<string,int>,
			depart_title MAP<STRING,ARRAY<STRING>>)
		ROW FORMAT DELIMITED
		FIELDS TERMINATED BY '|'
		COLLECTION ITEMS TERMINATED BY ','
		MAP KEYS TERMINATED BY ':';

	41.4 LOAD DATA LOCAL INPATH '/root/TrainingOnHDP/dataset/employee_id.txt' OVERWRITE INTO TABLE employee_id;

	41.5 CREATE TABLE IF NOT EXISTS employee_hr(
		name string,
		employee_id int,
		sin_number string,
		start_date date)
		ROW FORMAT DELIMITED
		FIELDS TERMINATED BY '|'
		STORED AS TEXTFILE;

	41.6 LOAD DATA LOCAL INPATH '/root/TrainingOnHDP/dataset/employee_hr.txt' OVERWRITE INTO TABLE employee_hr;	
	
42. Working with Complex Types
	42.1 The COLLECTION ITEMS TERMINATED BY char clause defines the delimiter character, e.g. ','
	42.2 The collection items can be struct members, array elements, and map key-value pairs
	42.3 For example, with a ',' used as the delimiting char:
		gender_age STRUCT<gender:string,age:int> will require that the underlying rows will have its elements grouped as follows:
		Male,30
		Female,27
		
		You access struct elements as follows:
		SELECT gender_age.gender, gender_age.age from employee;
		
		Query the whole struct and each struct column in the table:
		SELECT gender_age FROM employee;
		
		work_place ARRAY<INT> will require elements of the array be packed as follows:
		Montreal,Toronto
		
		To read the first element (Montreal) of the commands array in all rows:
		SELECT work_place[0] FROM employee;
	
		Query the whole array and each array column in the table:
		SELECT work_place FROM employee;
		
		SELECT work_place[0] AS col_1, work_place[1] AS col_2, work_place[2] AS col_3 FROM employee;
	
	
43. Working with Complex Types
	43.1 MAP KEYS TERMINATED BY ':' would require you separate keys from values with a ':'
	43.2 The COLLECTION ITEMS TERMINATED BY char clause controls the key-value pair separator
	43.3 The DELIMITED FIELDS TERMINATED BY char clause controls the field separator
	43.4 The decisions MAP<STRING, INT> definition would require the input file elements representing the map be grouped as follows:
			Python:80
			Test:Lead,COE:Architect
			
			In order to read values keyed, for example, by "Python" from the decision map:
			
			SELECT skills_score["Python"] FROM employee;
			
			
			Query the whole map and each map column in the table:
			
			SELECT skills_score FROM employee;
			
			SELECT name, skills_score['DB'] AS DB, skills_score['Perl'] AS Perl, skills_score['Python'] AS Python, skills_score['Sales'] as Sales, skills_score['HR'] as HR FROM employee;

43. Working with Complex Types
	43.1 HIVE nested ARRAY in MAP data type
			Query the composite type in the table:
			
			SELECT depart_title FROM employee;
			
			SELECT name, depart_title['Product'] AS Product, depart_title['Test'] AS Test, depart_title['COE'] AS COE, depart_title['Sales'] AS Sales FROM employee;
			
			SELECT name, depart_title['Product'][0] AS product_col0, depart_title['Test'][0] AS test_col0 FROM employee;

43. Data type conversions
	43.1 Hive supports both implicit type conversion and explicit type conversion
	43.2 Primitive type conversion from a narrow to a wider type is known as implicit conversion. However, the reverse conversion is not allowed. All the integral numeric types, FLOAT, and
		STRING can be implicitly converted to DOUBLE, and TINYINT, SMALLINT, and INT can all be converted to FLOAT. BOOLEAN types cannot be converted to any other type.
	43.3 Explicit type conversion is using the CAST function with the CAST(value AS TYPE) syntax.
		For example, CAST('100' AS INT) will convert the string 100 to the integer value 100. If
		the cast fails, such as CAST('INT' AS INT), the function returns NULL. In addition, the
		BINARY type can only cast to STRING, then cast from STRING to other types, if needed.	
		
		SELECT CAST(gender_age.age AS SMALLINT) AS age FROM employee;	
		
			
56. Example of Creating managed table
	56.1 Create the internal table and load the data
		CREATE TABLE IF NOT EXISTS employee_internal(
			name string,
			work_place ARRAY<string>,
			gender_age STRUCT<gender:string,age:int>,
			skills_score MAP<string,int>,
			depart_title MAP<STRING,ARRAY<STRING>>
		)
		COMMENT 'This is an internal table'
		ROW FORMAT DELIMITED
		FIELDS TERMINATED BY '|'
		COLLECTION ITEMS TERMINATED BY ','
		MAP KEYS TERMINATED BY ':'
		STORED AS TEXTFILE;
		
		LOAD DATA LOCAL INPATH '/root/TrainingOnHDP/dataset/employee.txt' OVERWRITE INTO TABLE employee_internal;
	
		
56. Example of Using EXTERNAL
	56.1 If you have an HDFS folder named /user/me/myexternal_folder/ which contains a text file with two tab-delimited columns, you can use the following CREATE EXTERNAL TABLE statement against this
		location:
		
		CREATE EXTERNAL TABLE tblExternal (col1 STRING, col2 STRING)
		COMMENT 'Cteating a table at a specific location'
		ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'
		STORED AS TEXTFILE 
		LOCATION '/user/me/myexternal_folder/";
		
		
57. Creating an Empty Table
	57.1 In some cases, you may need to have a table with the structure similar to that of an already existing table
	57.2 Use the following command to copy the table defition:
		CREATE TABLE T2 LIKE T1;
		
		In this case, the T2 table will be created with the same table dfinition used in creating the T1 table
		No records will be copied and T2 will be empty

58. Dropping a Table
	58.1 Use the following command to drop a table
		DROP TABLE [IF EXISTS] table_name
	
	58.2 The DROP TABLE statement removes table's metadata as well as the data in the table
	
	Note:
		The data is moved to the ./Trash/Current directory in the user's home HDFS directory (if trashing is configured);the table metadata in the metadstore can no longer be recovered
		When dropping a table created with the EXTERNAL parameter, the original data fille will not be deleted from the file system
		

72. Example of Loading Data into a Hive Table
	LOAD DATA LOCAL INPATH '/root/TrainingOnHDP/dataset/employee.txt' OVERWRITE INTO TABLE employee;
	

73. More examples

CREATE TABLE employee(
			name string		COMMENT 'name', 
			work_place ARRAY<string>	COMMENT 'Work Place',
			gender_age STRUCT<gender:string,age:int>	COMMENT 'Gender and Age',
			skills_score MAP<string,int>	COMMENT 'Skill Score',
			depart_title MAP<string,ARRAY<string>>	COMMENT 'Department and Title')
		ROW FORMAT DELIMITED FIELDS TERMINATED BY '|'
		COLLECTION ITEMS TERMINATED BY ','
		MAP KEYS TERMINATED BY ':';		
		
LOAD DATA INPATH '/user/root/exmployee1.txt' OVERWRITE INTO TABLE employee;	


74. Advanced Hive Features

